<template>
    <el-row :rules="rules" class="login-box">
        <el-col :lg="16" :md="12" class="left">
            <div>
                <div class="happy">
                    欢迎光临
                </div>
                <div class="desc">
                    本项目是基于vue3的商城后台开发
                </div>
            </div>
        </el-col>
        <el-col :lg="8" :md="12" class="right">
            <h2 class="back">欢迎回来</h2>
            <div>
                <span class="xiu"></span>
                <span>账号密码登录</span>
                <span class="xiu"></span>
            </div>
            <el-form ref="formRef" :model="form" :rules="rules" class="form-box">
                <el-form-item prop="username">
                    <el-input v-model="form.username" placeholder="请输入用户名"> <template #prefix>
                            <el-icon>
                                <User />
                            </el-icon>
                        </template></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" show-password v-model="form.password" placeholder="请输入密码"><template
                            #prefix>
                            <el-icon>
                                <Lock />
                            </el-icon>
                        </template></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button @click="onSubmit" round color="#626aef" :loading="loading" type="primary"
                        class="w-[250px] ">登录</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>
<script setup>
import { reactive, ref,onBeforeUnmount,onMounted } from 'vue'
import { Notify } from '@/composables/util.js'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'


const loading = ref(false)
const router = useRouter()
const store = useStore()

const form = reactive({
    username: '',
    password: '',
})
const formRef = ref(null)
const rules =
{
    username: [
        { required: true, message: '用户名不能为空', trigger: 'blur' },
        { min: 3, max: 5, message: '用户名长度不符合', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
        { min: 3, max: 5, message: '密码长度不符合', trigger: 'blur' },],
}

const onSubmit = () => {
    formRef.value.validate(
        (valid) => {
            console.log(valid);
            if (!valid) {
                return false
            }
            loading.value = true
            store.dispatch('login',form).then(()=>{
                Notify("登录成功")
                router.push('/')
            }).finally(()=>{
                loading.value=false
            })
            // login(form.username, form.password)
            //     .then(res => {
            //         console.log(res);
            //         // 提示成功
            //         Notify("登录成功")
            //         // 存储token
            //         setToken(res.token)
            //         // 获取用户信息

            //         // 跳转到后台首页
            //         router.push('/')
            //     })
        }
    )
}
// 监听回车事件
function onKeyup(e)
{
    if(e.key == 'Enter') onSubmit()

}
// 添加键盘监听
onMounted(()=>{
    document.addEventListener('keyup',onKeyup)

})
// 移除键盘监听
onBeforeUnmount(()=>{
    document.removeEventListener('keyup',onKeyup)
})



</script>



<style lang="scss" scoped>
.login-box {
    @apply bg-indigo-500 min-h-screen;
}

.login-box .left,
.login-box .right {
    @apply flex items-center justify-center;
}

.login-box .right {
    @apply bg-light-50 flex-col;
}

.left>div>.happy {
    @apply font-bold text-5xl text-light-50 mb-4;
}

.left>div>.desc {
    @apply text-gray-200 text-sm;
}

.right>.back {
    @apply font-bold text-3xl text-gray-800;

}

.right>div {
    @apply flex items-center justify-center my-5 text-gray-300 space-x-2;
}

.right>div .xiu {
    @apply h-[1px] w-16 bg-gray-200;
}

.form-box {
    @apply w-[250px]
}
</style>